﻿//Copyrigh (C) 2021 上海东欣软件工程有限公司 版权所有。
//<author>邢耀光</author>
//<data>2021.1.31</data>

using Core;
using Dal;
using System;
using System.Collections.Generic;
using System.Data;

namespace Service
{
    /// <summary>
    /// 部门信息服务类
    /// </summary>
    public class DepartmentService
    {
        /// <summary>
        ///  部门信息操作类
        /// </summary>
        private IDepartmentDao dao = DalService.DepartmentDao;

        /// <summary>
        /// 添加部门
        /// </summary>
        /// <param name="department"> 部门信息</param>
        /// <param name="connString">数据库连接字符串</param>
        /// <returns>是否添加成功</returns>
        public bool InsertDepartment(Department department, string connString)
        {
            //检测部门名称是否为空
            if (string.IsNullOrEmpty(department.Name))
            {
                throw new Exception("部门名称不能为空");
            }
            //添加部门
            try
            {
                return dao.InsertDepartment(department, connString) > 0;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        /// <summary>
        /// 修改部门
        /// </summary>
        /// <param name="department">部门信息</param>
        /// <param name="connString">数据库连接字符串</param>
        /// <returns>是否修改成功</returns>
        public bool UpdateDepartment(Department department, string connString)
        {
            //检测部门名称是否为空
            if (string.IsNullOrEmpty(department.Name))
            {
                throw new Exception("部门不名称能为空");
            }
            //修改部门
            try
            {
                return dao.UpdateDepartment(department, connString) > 0;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        /// <summary>
        /// 删除部门
        /// </summary>
        /// <param name="id">部门id</param>
        /// <param name="connString">数据库连接字符串</param>
        /// <returns>是否删除成功</returns>
        public bool DeleteDepartment(int id, string connString)
        {
            //检测部门是否为空
            if (id <= 0)
            {
                throw new Exception("部门不能为空");
            }
            //删除部门
            try
            {
                return dao.DeleteDepartment(id, connString) > 0;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        /// <summary>
        /// 模糊查询部门
        /// </summary>
        /// <param name="args">部门查询信息</param>
        /// <param name="connString">数据库连接字符串</param>
        /// <returns>查询结果集</returns>
        public List<Department> QueryDepartment(Department args, string connString)
        {
            //查询部门
            try
            {
                return dao.QueryDepartment(args,connString);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        /// <summary>
        /// 精确查询部门
        /// </summary>
        /// <param name="args">部门查询信息</param>
        /// <param name="connString">数据库连接字符串</param>
        /// <returns>查询结果集</returns>
        public List<Department> ExactQueryDepartment(Department args, string connString)
        {
            //查询部门
            try
            {
                return dao.ExactQueryDepartment(args, connString);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        /// <summary>
        /// 获取当前部门所有父级列表
        /// </summary>
        /// <param name="connString">数据库连接字符串</param>
        /// <param name="departmentID">当前部门ID</param>
        /// <returns>查询结果集</returns>
        public DataTable GetAncestorDepartments(string connString, int departmentID)
        {
            //检测部门是否为空
            if (departmentID <= 0)
            {
                throw new Exception("部门不能为空");
            }
            try
            {
                return dao.GetAncestorDepartments(connString, departmentID);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        /// <summary>
        /// 获取当前部门所有子级级列表
        /// </summary>
        /// <param name="connString">数据库连接字符串</param>
        /// <param name="departmentID">当前部门ID</param>
        /// <returns>查询结果集</returns>
        public DataTable GetDescendantDepartments(string connString, int departmentID)
        {
            //检测部门是否为空
            if (departmentID <= 0)
            {
                throw new Exception("部门不能为空");
            }
            try
            {
                return dao.GetDescendantDepartments(connString, departmentID);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        /// <summary>
        /// 根据部门名称判断是否存在
        /// </summary>
        /// <param name="connString">数据库连接字符串</param>
        /// <param name="departmentName">部门名称</param>
        /// <returns>是否存在</returns>
        public bool ExistDepartment(string connString, string departmentName)
        {
            //检测部门是否为空
            if (string.IsNullOrEmpty(departmentName))
            {
                throw new Exception("部门名称不能为空");
            }
            try
            {
                return dao.ExistDepartment(connString, departmentName);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        /// <summary>
        /// 判断当前部门下的部门名称是否存在
        /// </summary>
        /// <param name="connString">数据库连接字符串</param>
        /// <param name="id">当前部门id</param>
        /// <param name="departmentName">部门名称</param>
        /// <returns>是否存在</returns>
        public bool ExistDepartment(string connString, int id, string departmentName)
        {
            //检测部门是否为空
            if (string.IsNullOrEmpty(departmentName))
            {
                throw new Exception("部门名称不能为空");
            }
            try
            {
                return dao.ExistDepartment(connString, id, departmentName);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        /// <summary>
        /// 根据部门id判断是否存在子级部门
        /// </summary>
        /// <param name="connString">数据库连接字符串</param>
        /// <param name="departmentID">部门ID</param>
        /// <returns>是否存在</returns>
        public bool ExistDepartmentChildren(string connString, int departmentID)
        {
            //检测部门是否为空
            if (departmentID <= 0)
            {
                throw new Exception("部门不能为空");
            }
            try
            {
                return dao.ExistDepartmentChildren(connString, departmentID);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        /// <summary>
        /// 根据部门id判断当前部门是否是根部门
        /// </summary>
        /// <param name="connString">数据库连接字符串</param>
        /// <param name="departmentID">部门ID</param>
        /// <returns>是否存在</returns>
        public bool DepartmentIsRoot(string connString, int departmentID)
        {
            //检测部门是否为空
            if (departmentID <= 0)
            {
                throw new Exception("部门不能为空");
            }
            try
            {
                return dao.DepartmentIsRoot(connString, departmentID);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        /// <summary>
        /// 根据当前部门id获得下一级部门
        /// </summary>
        /// <param name="connString">数据库连接字符串</param>
        /// <param name="departmentID">部门ID</param>
        /// <returns>是否存在</returns>
        public List<Department> GetDepartmentChildrens(string connString, int departmentID)
        {
            //检测部门是否为空
            if (departmentID <= 0)
            {
                throw new Exception("部门不能为空");
            }
            try
            {
                return dao.GetDepartmentChildrens(connString, departmentID);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        /// <summary>
        /// 根据当前部门父id获得上一级部门
        /// </summary>
        /// <param name="connString">数据库连接字符串</param>
        /// <param name="departmentParentID">部门父ID</param>
        /// <returns>是否存在</returns>
        public List<Department> GetDepartmentParents(string connString, int departmentParentID)
        {
            //检测部门是否为空
            if (departmentParentID <= 0)
            {
                throw new Exception("部门不能为空");
            }
            try
            {
                return dao.GetDepartmentParents(connString, departmentParentID);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        /// <summary>
        /// 获取部门根节点信息
        /// </summary>
        /// <param name="connString">数据库连接字符串</param>
        /// <returns>部门根节点信息</returns>
        public Department GetDepartmentRoot(string connString)
        {            
            try
            {
                Department args = new Department();
                args.ParentID = 0;                
                return dao.QueryDepartment(args, connString)[0];
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
    }
}
